package ch.zbw.timeline.presentation.component;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import ch.zbw.timeline.domain.Process;
import ch.zbw.timeline.presentation.ClientEventListener;

/**
 * Administration panel for the single process
 * 
 * @author Daniel Stefanovic
 * @version 18.01.2012
 * 
 */
public class AdminProcessSinglePanel extends JPanel {

	private ClientEventListener listener;
	private JLabel lblDescription = new JLabel("Bezeichnung");
	private JLabel lblActive = new JLabel("Aktiv");
	private JTextField txtDescription = new JTextField();
	private JCheckBox chkActive = new JCheckBox();
	private JButton btnSave = new JButton("Speichern");
	private boolean editMode;
	private Process process;

	/**
	 * Constructor
	 */
	public AdminProcessSinglePanel(){
		this.editMode = false;
		init();
	}
	
	/**
	 * Constructor
	 * 
	 * @param editMode enable edit mode?
	 */
	public AdminProcessSinglePanel(boolean editMode) {
		this.editMode = editMode;
		init();
	}

	/**
	 * Initialization
	 */
	private void init() {
		JPanel wrapper = new JPanel();
		JPanel settingPanel = new JPanel();

		String borderText = editMode ? "Prozess bearbeiten" : "Neuen Prozess erstellen";
		Border loweredEtched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		wrapper.setBorder(BorderFactory.createTitledBorder(loweredEtched, borderText));
		wrapper.setLayout(null);

		btnSave.setBounds(200, 100, 200, 25);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (validateUserInputs()){
					saveProcess();
				}
			}
		});

		settingPanel.setBounds(100, 30, 400, 60);
		settingPanel.setLayout(new GridLayout(2, 2, 5, 5));

		settingPanel.add(lblDescription);
		settingPanel.add(txtDescription);
		settingPanel.add(lblActive);
		settingPanel.add(chkActive);
		wrapper.add(settingPanel);
		wrapper.add(btnSave);


		this.setLayout(new BorderLayout());
		this.add(wrapper);
	}

	public void setProcess(Process process) {
		this.process = process;
		loadData(process);
	}
	
	/**
	 * Loads the selected process in the form
	 * 
	 * @param process
	 *            Process to load
	 */
	private void loadData(Process process) {
		if (process != null) {
			this.txtDescription.setText(process.getDescription());
			this.chkActive.setSelected(process.getActive() == 1);
		}
	}

	/**
	 * Adds an EventListener to the panel
	 * 
	 * @param listener
	 *            EventListener
	 */
	public void addEventListener(ClientEventListener listener) {
		this.listener = listener;
	}

	/**
	 * Validate user inputs
	 * 
	 * @return valide?
	 */
	private boolean validateUserInputs() {
		boolean valid = true;
		Color red = new Color(255, 0, 0);
		Color white = new Color(255, 255, 255);

		// user name valide?
		if (txtDescription.getText().length() > 0) {
			txtDescription.setBackground(white);
		} else {
			valid = false;
			txtDescription.setBackground(red);
		}
		
		return valid;
	}

	/**
	 * Save current process
	 */
	private void saveProcess() {
		int active = chkActive.isSelected() ? 1 : 0;

		if(!editMode){
			process = new Process();
		}
		
		process.setDescription(txtDescription.getText());
		process.setActive(active);
		
		listener.saveProcessButtonPressed(editMode, process);

		this.clearAllFields();
	}
	
	/**
	 * Clear all input fields
	 */
	private void clearAllFields() {
		process = null;
		txtDescription.setText("");
	}
}
